<?php
/**
 * @ClassName: Credential
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-11 09:57
 * @Version: V1.0.0
 */

namespace app\api\controller\store;

use app\api\controller\Api;
use app\common\model;
use app\common\model\StoreCredential;

class Credential extends Api
{
    /**
     * 资质列表
     * @return \think\response\Json
     */
    function list()
    {
        $store_id = input('store_id', 0);
        $type = input('type');
        $status = input('status', -1);
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $where = [];
        $where[] = ['store_id', '=', $store_id];
        $where[] = ['is_show', '=', 1];

        if ($type) {
            $where[] = ['type', '=', $type];
        }

        if ($status != -1) {
            $where[] = ['status', '=', $status];
        }

        $list = model\StoreCredential::where($where)->order('sort asc, id asc')->limit($skip, $limit)->select();

        foreach ($list as &$item) {
            $item['certificate_files'] = stringToArray($item['certificate_file']);//资质文件列表
            $item['certificate_file_url'] = $item['certificate_files'][0];//封面图
            $item['type_str'] = model\StoreCredential::$certificate_types[$item['type']];
            $item['status_str'] = model\StoreCredential::$status_types[$item['status']];
            $item['checked'] = false;
        }

        $result = [
            'data' => $list,
            'is_more' => count($list) >= $limit,
        ];

        return json(["code" => 1, "msg" => "获取成功", "data" => $result]);
    }

    /**
     * 资质详情
     * @return \think\response\Json
     */
    function detail()
    {
        $id = input('id', 0);

        $data = model\StoreCredential::get($id);

        if ($data) {
            $data['certificate_files'] = stringToArray($data['certificate_file']);//资质文件列表
            $data['certificate_file_url'] = $data['certificate_files'][0];//封面图
        }

        return json(['code' => 1, 'msg' => 'ok', 'data' => $data]);
    }

    /**
     * 保存资质
     * @return \think\response\Json
     */
    function save()
    {
        $data = input();

        $add_data = [
            'store_id' => $data['store_id'],
            'shop_type' => $data['shop_type'],
            'name' => $data['name'],
            'type' => $data['type'],
            'certificate_file' => $data['certificate_file'],
            'certificate_expiry' => $data['certificate_expiry'],
            'certificate_validity_type' => $data['certificate_validity_type'],
            'status' => 0,//资质状态，0待审核，1审核通过，2审核拒绝，3已过期
            'remark' => $data['remark'],
        ];

        if (isset($data['id']) && $data['id']) {
            // 编辑
            $add_data['update_time'] = date_now();
            $res = model\StoreCredential::where('id', $data['id'])->update($add_data);
            if ($res) {
                $cre_id = $data['id'];
                $msg = '修改';
            } else {
                $cre_id = 0;
                $msg = '修改';
            }
        } else {
            // 新增
            $add_data['create_time'] = date_now();
            $add_data['update_time'] = date_now();
            $cre_id = model\StoreCredential::insertGetId($add_data);
            $msg = '新增';
        }

        if (!$cre_id) {
            return json(['code' => 0, 'msg' => $msg . '失败']);
        }

        return json(['code' => 1, 'msg' => $msg . '成功', 'data' => $cre_id]);
    }

    /**
     * 删除资质
     * @return \think\response\Json
     */
    function del()
    {
        $id = input('id', 0);

        $res = model\StoreCredential::where('id', $id)->update([
            'update_time' => date_now(),
            'delete_time' => date_now(),
        ]);

        if (!$res) {
            return json(['code' => 0, 'msg' => '删除失败']);
        }

        return json(['code' => 1, 'msg' => '删除成功']);
    }

    /**
     * 获取资质类型
     * @return \think\response\Json
     */
    function getCertificateType()
    {
        $certificate_types = [];

        foreach (StoreCredential::$certificate_types as $key => $item) {
            $certificate_types[] = [
                'name' => $item,
                'value' => $key,
                'disabled' => false,
            ];
        }

        return json(['code' => 1, 'data' => $certificate_types]);
    }

}